﻿Imports System.ComponentModel
Imports MapWinGIS
<Serializable()> Public Class SDSGridLineLayer
    Inherits SelfDrawShapeLayer





    Public Sub New(ByVal ln As String)
        MyBase.new(ln)


        m_XCells = 10
        m_YCells = 10
        m_LBPointXA = 0
        m_LBPointYA = 0

        m_XunitL = 1
        m_YuintL = 1

        m_IsShowXLine = True
        m_IsShowYline = True




        linetype = FtkLineStipple.lsNone
        lineColor = Drawing.Color.Black
        lineWidth = 1


    End Sub

    '这些都是为了讨厌的VB事件逻辑啊
    Protected Sub New( _
      ByVal info As System.Runtime.Serialization.SerializationInfo, _
      ByVal context As System.Runtime.Serialization.StreamingContext)

        MyBase.New(info, context)

    End Sub


    '线条相关以及对应的处理程序

    Dim linetype As FtkLineStipple

    <CategoryAttribute("外观设置"), _
   DisplayName("线条类型")> _
   Public Overridable Property LinetypeProperty() As FtkLineStipple
        Get
            Return linetype
        End Get
        Set(ByVal value As FtkLineStipple)
            If linetype = value Then

            Else

                linetype = value

                Me.OnLnTypeChanged(linetype)

            End If
           

        End Set
    End Property


    Dim lineColor As Drawing.Color
    <CategoryAttribute("外观设置"), _
    DisplayName("线条颜色")> _
    Public Overridable Property LineColorProperty() As Drawing.Color
        Get
            Return lineColor
        End Get
        Set(ByVal value As Drawing.Color)

            If lineColor = value Then

            Else

                lineColor = value
                Me.OnLnClChanged(lineColor)
            End If

        
        End Set
    End Property
    Dim lineWidth As Double

    <CategoryAttribute("外观设置"), _
   DisplayName("线条宽度")> _
   Public Overridable Property LineWidthProperty() As Double
        Get
            Return lineWidth
        End Get
        Set(ByVal value As Double)
            If lineWidth = value Then

            Else

                lineWidth = value

                Me.OnLnWdChanged(lineWidth)

            End If

           

        End Set
    End Property

    Private Sub OnLnWdChanged(ByVal d As Double)

        mapwin.set_ShapeLayerLineWidth(m_iLayer, d)
    End Sub
    Private Sub OnLnClChanged(ByVal cl As Drawing.Color)

        Dim ucl As UInt32 = RGB(cl.R, cl.G, cl.B)
        mapwin.set_ShapeLayerLineColor(m_iLayer, ucl)
    End Sub
    Private Sub OnLnTypeChanged(ByVal lt As MapWinGIS.tkLineStipple)

        Dim temp As MapWinGIS.tkLineStipple = Me.getLineType(lt)
        mapwin.set_ShapeLayerLineStipple(m_iLayer, temp)

    End Sub




    Public Sub SetGridProperty(ByVal m_LBPointXA As Double, ByVal m_LBPointYA As Double, ByVal m_XCells As Integer, ByVal m_YCells As Integer, ByVal m_XunitL As Double, ByVal m_YuintL As Double)

        Me.m_LBPointXA = m_LBPointXA
        Me.m_LBPointYA = m_LBPointYA

        'Me.m_LBPointX = m_LBPointX
        'Me.m_LBPointY = m_LBPointY

        Me.m_XCells = m_XCells
        Me.m_YCells = m_YCells

        Me.m_XunitL = m_XunitL
        Me.m_YuintL = m_YuintL

        Me.AllRedraw()

    End Sub


    '需要指定的属性

    Dim m_LBPointXA As Double
    <CategoryAttribute("特定设置"), _
    Browsable(False), _
 DisplayName("左下点绝对坐标X")> _
   Public Property LBPointXAProperty() As Double
        Get
            Return m_LBPointXA
        End Get
        Set(ByVal value As Double)
            If m_LBPointXA = value Then

            Else

                m_LBPointXA = value

                Me.AllRedraw()

            End If



        End Set
    End Property
    Dim m_LBPointYA As Double
    <CategoryAttribute("特定设置"), _
      Browsable(False), _
DisplayName("左下点绝对坐标Y")> _
   Public Property LBPointYAProperty() As Double
        Get
            Return m_LBPointYA
        End Get
        Set(ByVal value As Double)

            If m_LBPointYA = value Then

            Else

                m_LBPointYA = value
                Me.AllRedraw()

            End If



        End Set
    End Property




    Dim m_XCells As Integer
    <CategoryAttribute("特定设置"), _
      Browsable(False), _
DisplayName("X轴单元格数")> _
   Public Property XCellsProperty() As Integer
        Get
            Return m_XCells
        End Get
        Set(ByVal value As Integer)
            If m_XCells = value Then

            Else

                If value < 0 Then
                    value = 0
                End If
                m_XCells = value
                Me.AllRedraw()

            End If



        End Set
    End Property
    Dim m_YCells As Integer
    <CategoryAttribute("特定设置"), _
      Browsable(False), _
DisplayName("Y轴单元格数")> _
   Public Property YCellsProperty() As Integer
        Get
            Return m_YCells
        End Get
        Set(ByVal value As Integer)

            If m_YCells = value Then
            Else

                If value < 0 Then
                    value = 0
                End If
                m_YCells = value
                Me.AllRedraw()


            End If


        End Set
    End Property

    Dim m_XunitL As Double
    <CategoryAttribute("特定设置"), _
      Browsable(False), _
DisplayName("X轴单元格长度")> _
   Public Property XunitLProperty() As Double
        Get
            Return m_XunitL
        End Get
        Set(ByVal value As Double)

            If m_XunitL = value Then
            Else

                If value < 0 Then
                    value = 0
                End If
                m_XunitL = value
                Me.AllRedraw()

            End If



        End Set
    End Property

    Dim m_YuintL As Double
    <CategoryAttribute("特定设置"), _
      Browsable(False), _
DisplayName("Y轴单元格长度")> _
   Public Property YuintLProperty() As Double
        Get
            Return m_YuintL
        End Get
        Set(ByVal value As Double)
            If m_YuintL = value Then

            Else

                If value < 0 Then
                    value = 0
                End If
                m_YuintL = value
                Me.AllRedraw()

            End If



        End Set
    End Property

    Dim m_IsShowXLine As Boolean
    <CategoryAttribute("特定设置"), _
DisplayName("显示横向网格线")> _
  Public Property IsShowXLineProperty() As Boolean
        Get
            Return m_IsShowXLine
        End Get
        Set(ByVal value As Boolean)
            If m_IsShowXLine = value Then

            Else
                m_IsShowXLine = value
                Me.AllRedraw()

            End If
           

        End Set
    End Property
    Dim m_IsShowYline As Boolean
    <CategoryAttribute("特定设置"), _
DisplayName("显示纵向网格线")> _
  Public Property IsShowYlineProperty() As Boolean
        Get
            Return m_IsShowYline
        End Get
        Set(ByVal value As Boolean)
            If m_IsShowYline = value Then
            Else
                m_IsShowYline = value
                Me.AllRedraw()

            End If
            

        End Set
    End Property



    Protected Overrides Sub DrawMySelf()
        If Me.IsShowXLineProperty Then
            Me.DrawGridLineX()
        End If

        If Me.IsShowYlineProperty Then
            Me.DrawGridLineY()
        End If


    End Sub

    Protected Overrides Sub DrawText()

    End Sub

    Protected Sub DrawGridLineX()

        Dim xOrg As Double = Me.LBPointXAProperty
        Dim yOrg As Double = Me.LBPointYAProperty
        Dim xcells As Integer = Me.XCellsProperty
        Dim ycells As Integer = Me.YCellsProperty

        'Dim gridspace As Double
        Dim xspace As Double = Me.XunitLProperty
        Dim yspace As Double = Me.YuintLProperty


        Dim sfGrid As Shapefile

        sfGrid = mapwin.get_GetObject(m_iLayer)



        '以下命名规则为，与x轴相交的点的集合，叫做xpts

        ' Dim nx As Integer = (xcells + 1) * 2
        Dim ny As Integer = (ycells + 1) * 2

        ' Dim xpts(nx) As MapWinGIS.Point

        Dim ypts(ny) As MapWinGIS.Point

        Dim pt1 As MapWinGIS.Point
        Dim pt2 As MapWinGIS.Point



        For j As Integer = 0 To ycells


            pt1 = New Point()
            pt1.x = xOrg
            pt1.y = yOrg + j * yspace
            ypts(2 * j) = pt1

            pt2 = New Point()
            pt2.x = xcells * xspace + xOrg
            pt2.y = yOrg + j * yspace
            ypts(2 * j + 1) = pt2




        Next

        sfGrid.StartEditingShapes(True)




        For h As Integer = 0 To ycells

            Dim shp As New Shape
            shp.ShapeType = ShpfileType.SHP_POLYLINE
            Dim ptIndex As Integer = 0

            shp.InsertPoint(ypts(2 * h), ptIndex)
            shp.InsertPoint(ypts(2 * h + 1), ptIndex)




            Dim shpIndex As Integer = sfGrid.NumShapes

            sfGrid.EditInsertShape(shp, shpIndex)

            sfGrid.EditCellValue(0, shpIndex, shpIndex)

        Next




        sfGrid.StopEditingShapes()
        mapwin.Redraw()
    End Sub
    Protected Sub DrawGridLineY()

        Dim xOrg As Double = Me.LBPointXAProperty
        Dim yOrg As Double = Me.LBPointYAProperty
        Dim xcells As Integer = Me.XCellsProperty
        Dim ycells As Integer = Me.YCellsProperty

        'Dim gridspace As Double
        Dim xspace As Double = Me.XunitLProperty
        Dim yspace As Double = Me.YuintLProperty


        Dim sfGrid As Shapefile

        sfGrid = mapwin.get_GetObject(m_iLayer)




        '以下命名规则为，与x轴相交的点的集合，叫做xpts

        Dim nx As Integer = (xcells + 1) * 2
        ' Dim ny As Integer = (ycells + 1) * 2

        Dim xpts(nx) As MapWinGIS.Point

        ' Dim ypts(ny) As MapWinGIS.Point

        Dim pt1 As MapWinGIS.Point
        Dim pt2 As MapWinGIS.Point

        For i As Integer = 0 To xcells

            pt1 = New Point()
            pt1.x = xOrg + i * xspace
            pt1.y = yOrg
            xpts(2 * i) = pt1

            pt2 = New Point()
            pt2.x = xOrg + i * xspace
            pt2.y = (ycells) * yspace + yOrg
            xpts(2 * i + 1) = pt2




        Next



        sfGrid.StartEditingShapes(True)

        For k As Integer = 0 To xcells

            Dim shp As New Shape
            shp.ShapeType = ShpfileType.SHP_POLYLINE
            Dim ptIndex As Integer = 0

            shp.InsertPoint(xpts(2 * k), ptIndex)
            shp.InsertPoint(xpts(2 * k + 1), ptIndex)




            Dim shpIndex As Integer = sfGrid.NumShapes

            sfGrid.EditInsertShape(shp, shpIndex)

            sfGrid.EditCellValue(0, shpIndex, shpIndex)

        Next







        sfGrid.StopEditingShapes()
        mapwin.Redraw()

    End Sub




    Public Overrides Sub OnExtentsChanged(ByVal sender As Object, ByVal e As System.EventArgs)

    End Sub

    Public Overrides Sub InitNewLayer(ByRef m As AxMapWinGIS.AxMap, ByVal OutPath As String)

        Dim sf As New Shapefile
        Dim strPath(2) As String


        'strPath(0) = "C:\temp\" + lname + ".shp"
        'strPath(1) = "C:\temp\" + lname + ".dbf"
        'strPath(2) = "C:\temp\" + lname + ".shx"

        'If System.IO.File.Exists(strPath(0)) Then
        '    System.IO.File.Delete(strPath(0))
        '    System.IO.File.Delete(strPath(1))
        '    System.IO.File.Delete(strPath(2))

        'End If

        sf.CreateNew("", MapWinGIS.ShpfileType.SHP_POLYLINE)




        Dim f As New Field

        f = New Field
        f.Name = "Name"
        f.Type = FieldType.STRING_FIELD
        f.Width = 50

        sf.StartEditingShapes(True)
        sf.EditInsertField(f, 0)
        sf.StopEditingShapes(True, True)


        '加入载入的逻辑

        Dim strSave As String = OutPath + lname + ".shp"

        strPath(0) = OutPath + lname + ".shp"
        strPath(1) = OutPath + lname + ".dbf"
        strPath(2) = OutPath + lname + ".shx"

        For i As Integer = 0 To strPath.Length - 1
            If System.IO.File.Exists(strPath(i)) Then
                Try
                    System.IO.File.SetAttributes(strPath(i), IO.FileAttributes.Normal)
                    System.IO.File.Delete(strPath(i))
                Catch ex As Exception

                End Try

            End If
        Next

        Dim bS As Boolean = sf.SaveAs(strSave)

        If Not bS Then
            System.Windows.Forms.MessageBox.Show(lname + "文件创建失败！，请查看电脑软件使用权限")

        End If

        fname = strSave
        Me.InitLayer(m)

    End Sub

    Public Overrides Sub InitProp()

        Me.OnLnClChanged(lineColor)
        Me.OnLnWdChanged(lineWidth)
        Me.OnLnTypeChanged(linetype)

    End Sub
End Class
